# _*_ coding:utf-8 _*_
# @Time      : 23:02
# @Author    :baizhou
import uuid

import cv2
import numpy as np
import matplotlib.pyplot as plt
from django.conf import settings
import os
class ImageFilter(object):

    def D_Conolution(self, path):
        '''
        对图像进行锐化
        :param path: 文件路径
        :return: 返回处理后的图像
        '''
        img = cv2.imread(path)
        kernel = np.ones((5, 5), np.float32) / 25
        dstImage = cv2.filter2D(img, -1, kernel)
        return dstImage

    def avgImageFilter(self, path):
        '''
        图像模糊，均值滤波
        :param path: 文件路径
        :return: 返回处理后的图像
        '''
        img = cv2.imread(path)
        avgImage = cv2.blur(img, (5, 5))
        upload_dir = os.path.join(settings.DEAL_DIR)
        suffix_file = path.split(".")[-1]
        # 文件名
        file_name = str(uuid.uuid1()) + "." + suffix_file
        save_path = os.path.join(upload_dir,file_name)
        cv2.imwrite(save_path,avgImage)
        return file_name

    def gaussianImageFilter(self, path):
        '''
        高斯滤波
        :param path: 文件路径
        :return: 返回处理后的图像
        '''
        img = cv2.imread(path)
        gaussianImage = cv2.GaussianBlur(img, (5, 5), 0)
        upload_dir = os.path.join(settings.DEAL_DIR)
        suffix_file = path.split(".")[-1]
        # 文件名
        file_name = str(uuid.uuid1()) + "." + suffix_file
        save_path = os.path.join(upload_dir, file_name)
        cv2.imwrite(save_path, gaussianImage)
        return gaussianImage

    def medianImageFilter(self, path):
        '''
        中值滤波
        :param path: 文件路径
        :return: 返回处理后的图像
        '''
        img = cv2.imread(path)
        medianImage = cv2.medianBlur(img, 5)
        upload_dir = os.path.join(settings.DEAL_DIR)
        suffix_file = path.split(".")[-1]
        # 文件名
        file_name = str(uuid.uuid1()) + "." + suffix_file
        save_path = os.path.join(upload_dir, file_name)

        cv2.imwrite(save_path, medianImage)
        return file_name

    def bilateralImageFilter(self, path):
        '''
        双边滤波
        :param path: 文件路径
        :return: 返回处理后的图像
        '''
        img = cv2.imread(path)
        bilateralImage = cv2.bilateralFilter(img, 9, 75, 75)
        return bilateralImage